declare var form: any;
declare var map: any;

/**
 * 定义GIS地图加载辅助类
 */
export class GisSearchUtils {

    private fs: any;
    // 地图数据
    private mapData: any;
    // 当前地图使用方案
    private curMap: string;
    // 此次操作是否为切换图层之后第一次
    private changeMap: boolean;
    // 属性查询对象
    private ss: any = null;
    // 元数据
    metedata: string;
    // 所查询图层
    private layers = [];
    // 是否选中图层
    private isSelectMap = false;
    // 选中图层的id
    private featureId = '';
    // sourceId
    private sourceId = '';
    private sourceLayer = '';

    constructor() {
        this.fs = form.FormDataService;
    }



    init() {
        this.layersInit();

        const pmap = form.ConfigService.map;
        // 图层点击事件绑定
        pmap.on('click', (o: any) => {
            this.isSelectMap = false;
            // 变更方案后重新初始化数据
            if (this.curMap !== form.ConfigService.mapParas.styleId) {
                this.changeMap = true;
                this.layersInit();
            } else {
                this.changeMap = false;
            }

            const pixel = [o.point.x, o.point.y];
            // 按点线面图层id属性依次找
            // 使用some遍历直到某个条件符合返回true跳出循环
            this.layers.some(layer => {
                const feature = pmap.queryRenderedFeaturesByPixel(pixel, layer);
                if (feature && feature.length > 0) {
                    this.isSelectMap = true;
                    this.featureId = feature[0].properties.objectid;

                    this.sourceLayer = feature[0].sourceLayer;

                    // 弹窗id
                    const infoTemplateId = feature[0].source;
                    const ft: any = {};

                    const that = this;

                    this.mapData.sources.forEach(data => {
                        if (data.name === feature[0].layer.source) {
                            this.metedata = data.metedata;
                        }
                    });

                    // 组装feature对象
                    ft.type = feature[0].type;
                    ft.geometry = feature[0].geometry;
                    ft.properties = feature[0].properties;

                    if (this.ss) {
                        // 如果选择图层与上次不一样则查询服务重新初始化
                        if (feature[0].source !== this.sourceId) {
                            this.ss.init('click_attr', { infoTemplateId });
                        }
                        this.sourceId = feature[0].source;
                        this.showData(ft);
                    } else {
                        this.initSearchService(infoTemplateId, (service) => {
                            that.ss = service;
                            this.sourceId = feature[0].source;
                            that.showData(ft);
                        });
                    }
                    return true;
                }
            });

            // 点击空白处清除高亮
            if (this.isSelectMap === false) {
                if (this.ss) {
                    this.ss.clear();
                }
            }
        });
    }

    // 图层初始化
    layersInit() {

        // 地图数据获取
        this.mapData = form.ConfigService.mapParas;
        // 当前地图方案
        this.curMap = this.mapData.styleId;

        this.layers = [];
        const newLayers: any = [];
        // 可查询的图层筛选
        this.mapData.layers.forEach(data => {
            if (data.leaf === true && data.type !== null) {
                this.mapData.sources.forEach(source => {
                    if (source.name === data.source && source.canSearch === true) {
                        newLayers.push(data);
                    }
                });
            }
        });
        // 图层排序
        newLayers.sort((a, b) => {
            a.type.charCodeAt(0) - b.type.charCodeAt(0);
        });

        for (let i = newLayers.length - 1; i >= 0; i--) {
            this.layers.push(newLayers[i].id);
        }
    }

    // 查询访问初始化
    private initSearchService(infoTemplateId: string, callback?: any) {
        map.Factory.SimpleSearch('AttributeSearch', (attributeSearch) => {
            attributeSearch.init('click_attr', { infoTemplateId });
            if (callback) {
                callback(attributeSearch);
            }
        });
    }

    // 数据展示
    private showData(ft: any) {
        this.clear();

        if (ft.geometry.type !== 'Point' || ft.geometry.type !== 'MultiPoint') {
            const option = {
                layout: null,
                paint: null,
                filter: ['in', 'objectid', this.featureId],
                sourceId: this.sourceId,
                sourceLayer: this.sourceLayer
            };
            this.ss.lineHighlight(ft, option);
        } else {
            this.ss.highlight(ft);
        }

        // 翻译
        this.fs.dictionaryEscape(this.metedata, ft.properties, (data) => {
            for (const key in data) {
                if (data.hasOwnProperty(key) === true) {
                    if (key.indexOf('_Name') !== -1) {
                        const newKey = key.replace('_Name', '');
                        data[newKey] = data[key];
                        delete data[key];
                    }
                }
            }
            ft.properties = data;
            this.getProperties(ft.properties);
            this.ss.info(ft);
        });
    }

    // 属性处理
    getProperties(obj: any): any {
        // 转数字类型
        for (const key in obj) {
            if (obj.hasOwnProperty(key) === true) {
                const value = Number(obj[key]);
                if (!isNaN(value)) {
                    obj[key] = value;
                }
            }
        }
    }

    clear() {
        if (this.ss) {
            this.ss.clear();

            // 切换方案render服务需要重新初始化
            if (this.changeMap === true) {
                this.ss.resetRenderService();
            }
        }
    }
}
